import {
    getToken,
    setToken,
    removeToken,
} from '@/util/auth';
import {
    setStore,
    getStore,
    removeStore,
} from '@/util/store';
import {
    validatenull,
} from '@/util/validate';
import {
    encryption,
} from '@/util/util';
import {
    loginByCode,
    getUserInfo,
    getTableData,
    logout,
    getMenuAll,
    queryUserByUnionId,
    signIn,
    refeshToken,
} from '@/api/user';

const user = {
  state: {
      userInfo: getStore({
          name: 'userInfo',
        }) || {},
      unionId: '',
      permission: {},
      roles: getStore({
          name: 'roles',
        }) || '',
      community: getStore({
          name: 'community',
        }) || {},
      menu: [],
      menuAll: [],
      user: getStore({
          name: 'user',
        }) || {},
      token: getStore({
          name: 'token',
        }) || '',
      refershToken: getStore({
          name: 'refershToken',
        }) || '',
    },
  actions: {
        // 根据用户名登录
      LoginByUsername({
            commit,
            state,
            dispatch,
        }, userInfo) {
          return new Promise((resolve, reject) => {
              loginByUsername(userInfo.username, userInfo.password).then((res) => {
                  const data = res.data;
                  commit('SET_USER', userInfo);
                  commit('SET_TOKEN', data.data);
                  commit('DEL_ALL_TAG');
                  commit('CLEAR_LOCK');
                  setToken(data);
                  resolve();
                });
            });
        },
      getAuth({ commit, state, dispatch }) {
          return new Promise((resolve, reject) => {
              queryUserByUnionId(state.unionId).then((res) => {
                  const data = res.data;
                  if (!validatenull(data)) {
                      commit('SET_TOKEN', data.access_token);
                      commit('SET_REFERSH_TOKEN', data.refresh_token);
                      return dispatch('GetUserInfo');
                    }
                  resolve(res);
                }).then((res) => {
                  resolve(res);
                });
            });
        },
      loginByreg({
            commit,
            state,
            dispatch,
        }, params) {
          return new Promise((resolve, reject) => {
              bindUser(params.username, params.mobileCode, params.openId).then((res) => {
                  const data = res.data;
                  commit('SET_TOKEN', data.data);
                  setToken(data);
                  resolve(data);
                });
            });
        },
        // 根据手机号登录
      LoginByPhone({
            commit,
            state,
            dispatch,
        }, userInfo) {
          return new Promise((resolve, reject) => {
              loginByCode(userInfo.username, userInfo.mobileCode).then((res) => {
                  const data = res.data;
                  commit('SET_TOKEN', data.access_token);
                  commit('SET_REFERSH_TOKEN', data.refresh_token);
                  setToken(data.access_token);
                  resolve();
                }).catch((error) => {
                  console.log(error);
                });
            });
        },
      RefeshToken({
            commit,
            state,
            dispatch,
        }, param) {
          return new Promise((resolve, reject) => {
              refeshToken(state.refershToken).then((res) => {
                  const data = res.data;
                  commit('SET_TOKEN', data.access_token);
                  commit('SET_REFERSH_TOKEN', data.refresh_token);
                  setToken(data.access_token);
                  resolve();
                });
            });
        },
      GetTableData({
            commit,
            state,
            dispatch,
        }, page) {
          return new Promise((resolve, reject) => {
              getTableData(page).then((res) => {
                  const data = res.data;
                  resolve(data);
                });
            });
        },
      GetUserInfo({
            commit,
            state,
            dispatch,
        }) {
          return new Promise((resolve, reject) => {
              getUserInfo().then((res) => {
                  const data = res.data.obj;
                  if (!validatenull(data)) {
                      commit('SET_USERIFNO', data);
                      if (!validatenull(data.communityId)) {
                          commit('SET_COMMUNITY', {
                              communityId: data.communityId,
                              communityName: data.communityName,
                            });
                        }
                      commit('SET_ROLES', data.userType);
                    }
                  resolve(data);
                });
            });
        },
        // 登出
      LogOut({
            commit,
            state,
        }) {
          return new Promise((resolve, reject) => {
              logout().then(() => {
                  commit('SET_TOKEN', '');
                  commit('SET_REFERSH_TOKEN', '');
                  commit('SET_USER', {});
                  commit('SET_USERIFNO', {});
                  commit('SET_COMMUNITY', {});
                  commit('SET_ROLES', '');
                  removeToken();
                  resolve();
                }).catch((error) => {
                  reject(error);
                });
            });
        },
        // 注销session
      FedLogOut({
            commit,
            state,
        }) {
          return new Promise((resolve) => {
              commit('SET_TOKEN', '');
              commit('SET_REFERSH_TOKEN', '');
              commit('SET_USER', {});
              commit('SET_USERIFNO', {});
              commit('SET_COMMUNITY', {});
              commit('SET_ROLES', '');
              removeToken();
              resolve();
            });
        },
        // 获取系统菜单
      GetMenu({
            commit,
        }, parentId) {
          parentId;
          return new Promise((resolve) => {
              getMenu(parentId).then((res) => {
                  const data = res.data;
                  commit('SET_MENU', data);
                  resolve(data);
                });
            });
        },
        // 获取全部菜单
      GetMenuAll({
            commit,
        }) {
          return new Promise((resolve) => {
              getMenuAll().then((res) => {
                  const data = res.data;
                  commit('SET_MENU_ALL', data);
                  resolve(data);
                });
            });
        },

    },
  mutations: {
      SET_COMMUNITY: (state, community) => {
          state.community = community;
          setStore({
              name: 'community',
              content: state.community,
            });
        },
      SET_USER: (state, user) => {
          state.user = user;
          setStore({
              name: 'user',
              content: state.user,
            });
        },
      SET_REFERSH_TOKEN: (state, refershToken) => {
          state.refershToken = refershToken;
          setStore({
              name: 'refershToken',
              content: state.refershToken,
            });
        },
      SET_TOKEN: (state, token) => {
          state.token = token;
          setStore({
              name: 'token',
              content: state.token,
            });
        },
      SET_USERIFNO: (state, userInfo) => {
          state.userInfo = userInfo;
          setStore({
              name: 'userInfo',
              content: state.userInfo,
            });
        },
      SET_MENU: (state, menu) => {
          const list = menu.filter((ele) => {
              if (validatenull(ele.meta)) return true;
              if (validatenull(ele.meta.roles)) return true;
              if (ele.meta.roles.indexOf(state.roles[0]) != -1) {
                  return true;
                }
              return false;
            });
          state.menu = list;
        },
      SET_MENU_ALL: (state, menuAll) => {
          state.menuAll = menuAll;
        },
      SET_ROLES: (state, roles) => {
          state.roles = roles;
          setStore({
              name: 'roles',
              content: state.roles,
            });
        },
      SET_UNIONID: (state, unionId) => {
          state.unionId = unionId;
        },
      SET_PERMISSION: (state, permission) => {
          state.permission = {};
          permission.forEach((ele) => {
              state.permission[ele] = true;
            });
        },
    },

};
export default user;
